import React from 'react';
import { Input } from 'antd';

const DiyInputNumber: React.FC<IDiyInputNumberProps> = (props) => {
  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    let inputValue = e.target.value;

    inputValue = inputValue.replace(/[^0-9.]/g, '');

    const parts = inputValue.split('.');
    if (parts.length > 2) {
      inputValue = parts[0] + '.' + parts.slice(1).join('');
    }

    if (inputValue && parseFloat(inputValue) < 0) {
      inputValue = '0';
    }

    props.onChange && props.onChange(inputValue);
  };
  return <Input onChange={onChange} value={props.value} />;
};

interface IDiyInputNumberProps {
  value?: any;
  onChange?: (value: any) => void;
}

export default DiyInputNumber;
